<template>
  <div class="params-page">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert
        title="注意：只允许为第三级分类设置相关参数"
        type="warning"
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-row>
        <el-col>
          <span>选择商品分类：</span>
          <el-cascader
            :options="list"
            :props="cascaderProps"
            v-model="selectedKeys"
            @change="changeFn"
          ></el-cascader>
        </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-row>
            <el-button
              type="primary"
              size="small"
              @click="showFn('dynamic')"
              :disabled="selectedKeys.length !== 3"
              >添加动态参数</el-button
            >
          </el-row>
          <el-table border :data="attribute">
            <el-table-column label="参数名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="small"
                  @click="showFn('edit', row)"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="small"
                  @click="delFn(row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-row>
            <el-button
              type="primary"
              size="small"
              @click="showFn('static')"
              :disabled="selectedKeys.length !== 3"
              >添加静态属性</el-button
            >
          </el-row>
          <el-table border :data="attribute">
            <el-table-column label="参数名称" prop="attr_name">
            </el-table-column>
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="small"
                  @click="showFn('edit11', row)"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="small"
                  @click="delFn(row)"
                  >删除</el-button
                >
              </template></el-table-column
            >
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <el-dialog :visible.sync="isDialogShow" :title="title" @close="diaLogClose">
      <el-form label-width="90px" :rules="rules" :model="formData" ref="form">
        <el-form-item :label="labelTitle" prop="attr_name">
          <el-input v-model.lazy="formData.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button>取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  addParamsApi,
  delParamsApi,
  editParamsApi,
  getCategoriesApi,
  getClassificationApi,
} from "@/api/e-commerce";
import { Message } from "element-ui";

export default {
  name: "ParamsIndex",
  data() {
    return {
      attribute: [],
      activeName: "many",
      list: [],
      isDialogShow: false,
      diaType: "",
      title: "",
      labelTitle: "",
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true,
      },
      selectedKeys: "",
      formData: {
        attr_name: "",
      },
      rules: {
        attr_name: [{ required: true, message: "请输入分类名称" }],
      },
    };
  },
  created() {
    this.getSelectionList();
  },
  computed: {
    cateId() {
      if (this.selectedKeys.length === 3) {
        return this.selectedKeys[2];
      } else {
        return null;
      }
    },
  },
  methods: {
    async delFn(row) {
      console.log("删除");
      await this.$confirm("确定删除该参数吗？");
      await delParamsApi(this.cateId, row.attr_id);
      // console.log(this.cateId);
      // console.log(row);
      // console.log(res);
      Message.success("删除成功");
      this.getParamsData();
    },
    async submit() {
      this.$refs.form.validate();
      if (this.diaType === "dynamic" || this.diaType === "static") {
        // console.log("添加");
        await addParamsApi(this.cateId, {
          attr_name: this.formData.attr_name,
          attr_vals: this.formData.attr_vals,
          attr_sel: this.activeName,
        });
        // console.log(res);
        Message.success("添加成功");
      } else {
        // console.log("修改");
        await editParamsApi(this.cateId, this.formData.cat_id, {
          attr_name: this.formData.attr_name,
          attr_vals: this.formData.attr_vals,
          attr_sel: this.activeName,
        });
        Message.success("修改成功");
      }
      this.isDialogShow = false;
      this.getParamsData();
    },
    diaLogClose() {
      this.$refs.form.resetFields();
      this.formData = {};
    },
    async getParamsData() {
      if (this.selectedKeys.length !== 3) {
        this.selectedKeys = [];
        return;
      } else {
        // console.log(this.selectedKeys);
        // console.log(this.selectedKeys[2]);
        const res = await getCategoriesApi(this.cateId, {
          sel: this.activeName,
        });
        // console.log(res);
        this.attribute = res.data.data;
        // console.log(res.data.data);
      }
    },
    changeFn() {
      // console.log(this.selectedKeys);
      this.getParamsData();
    },
    async getSelectionList() {
      const res = await getClassificationApi({
        type: 3,
      });
      // console.log(res);
      this.list = res.data.data;
    },
    showFn(type, row) {
      this.isDialogShow = true;
      this.diaType = type;
      console.log(row);
      if (type === "dynamic") {
        this.title = "添加动态参数";
        this.labelTitle = "动态参数";
      } else if (type === "static") {
        this.title = "添加静态属性";
        this.labelTitle = "静态属性";
      } else if (type === "edit") {
        this.title = "修改动态参数";
        this.labelTitle = "动态参数";
        // this.formData.attr_name = row.attr_name;
        this.formData = row;
      } else {
        this.title = "修改静态属性";
        this.labelTitle = "静态属性";
        // this.formData.attr_name = row.attr_name;
        this.formData = row;
      }
    },
    handleClick() {
      this.getParamsData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-breadcrumb,
el-alert {
  margin-bottom: 15px;
}
.el-alert {
  margin-bottom: 15px;
}
.el-row {
  margin-bottom: 10px;
}
</style>
